package com.example.wishstar.viewmodel;

import android.app.DatePickerDialog;
import android.content.Context;
import android.icu.util.Calendar;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;

import androidx.databinding.BaseObservable;
import androidx.databinding.ObservableField;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.example.wishstar.MyApplication;
import com.example.wishstar.model.CommonDate;
import com.example.wishstar.model.NewMemory;
import com.example.wishstar.repository.AddMemoryRepository;
import com.example.wishstar.repository.UserRepository;

import java.time.LocalDate;

public class AddMemoryViewModel {
    private String TAG = "AddMemoryViewModel";
    private ObservableField<NewMemory> newMemoryObservableField;
    private Context context;
    private AddMemoryRepository addMemoryRepository;
    private int userId;
    private LocalDate nowDate;
    private IsRepeatCallBack isRepeatCallBack;
    private MutableLiveData<CommonDate> date;

    public MutableLiveData<CommonDate> getDate() {
        if (date == null) {
           date = new MutableLiveData<CommonDate>();
           String year = "" + nowDate.getYear();
           String month = "" + nowDate.getMonthValue();
           String day = "" + nowDate.getDayOfMonth();
           date.setValue(new CommonDate(year, month, day));
        }
        return date;
    }

    public interface IsRepeatCallBack {
        void callback(int isRepeat, int notRepeat);
    }

    public void setIsRepeatCallBack(IsRepeatCallBack isRepeatCallBack) {
        this.isRepeatCallBack = isRepeatCallBack;
    }

    public AddMemoryViewModel(Context context) {
        this.context = context;
        userId = MyApplication.userId;
        nowDate = LocalDate.now();
        NewMemory newMemory = new NewMemory(userId, nowDate);

        newMemoryObservableField = new ObservableField<>();
        newMemoryObservableField.set(newMemory);
        addMemoryRepository = new AddMemoryRepository(MyApplication.getApi());
    }

    public String getMemoryTitle() {
        return newMemoryObservableField.get().memoryTitle;
    }

    public void setMemoryTitle(String memoryTitle) {
        newMemoryObservableField.get().memoryTitle = memoryTitle;
    }

    public void repeat(View view) {
        NewMemory newMemory = newMemoryObservableField.get();
        if (newMemory.memoryDate.isAfter(nowDate)) {
            Toast.makeText(context, "正计时不需设置是否重复", Toast.LENGTH_SHORT).show();
        }
        else {
            newMemoryObservableField.get().isRepeat = 1;
            isRepeatCallBack.callback(1, 0);
        }
    }

    public void notRepeat(View view) {
        NewMemory newMemory = newMemoryObservableField.get();
        if (newMemory.memoryDate.isAfter(nowDate)) {
            Toast.makeText(context, "正计时不需设置是否重复", Toast.LENGTH_SHORT).show();
        }
        else {
            newMemoryObservableField.get().isRepeat = 0;
            isRepeatCallBack.callback(0, 1);
        }

    }

    public void check() {
        NewMemory newMemory = newMemoryObservableField.get();
        if (newMemory.memoryDate.isAfter(nowDate)) {
            newMemory.type = 2;
            isRepeatCallBack.callback(0, 0);
        }
        else {
            newMemory.type = 1;
            newMemory.isRepeat = 1;
            isRepeatCallBack.callback(1, 0);
            Toast.makeText(context, "请选择是否按年重复", Toast.LENGTH_SHORT).show();
        }
    }

    public void post() {
        NewMemory newMemory = newMemoryObservableField.get();
        Log.e(TAG, newMemory.toString());
        addMemoryRepository.addMemory(newMemory);
    }

    public void showDatePickerDialog(View view) {
        Calendar calendar=Calendar.getInstance();
        int themeResId = 3;
        new DatePickerDialog( context, themeResId,  new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                CommonDate commonDate = new CommonDate(String.valueOf(year), String.valueOf(month + 1), String.valueOf(dayOfMonth));
                date.setValue(commonDate);
                newMemoryObservableField.get().setMemoryDate(LocalDate.of(year, month + 1, dayOfMonth));
                check();
            }
        }
                ,calendar.get(Calendar.YEAR)
                ,calendar.get(Calendar.MONTH)
                ,calendar.get(Calendar.DAY_OF_MONTH)).show();
    }
}
